﻿using Barcode_Project.Entity;
using Barcode_Project.Local;
using Barcode_Project.Network;
using DevExpress.XtraEditors;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;

namespace Barcode_Project.CutPrint
{
    public partial class CutBarCodeSelector : DevExpress.XtraEditors.XtraForm
    {
        string plCode = "";
        string gxCode = "";
        string spec = "";

        public NeedCutBarCode selectedBarInfo { get; set; }

        public List<NeedCutBarCode> outputBarCodes { get; set; }

        public CutBarCodeSelector(string plCode, string gxCode, string spec)
        {
            this.plCode = plCode;
            this.gxCode = gxCode;
            this.spec = spec;

            InitializeComponent();


        }
        private void CutBarCodeSelector_Load(object sender, EventArgs e)
        {
            try
            {
                this.LoadBarCodes();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        List<NeedCutBarCode> oriBarCodes;
        void LoadBarCodes()
        {
            oriBarCodes = new RemoteRequest().GetPlanGxBarCodes(plCode, gxCode, spec);

            gridControl_barCodes.DataSource = oriBarCodes;
            gridControl_barCodes.RefreshDataSource();
        }

        private void button_confirm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (gridView_barCodes.GetSelectedRows().Count() == 0)
                {
                    XtraMessageBox.Show("请勾选条码");
                    return;
                }



                groupControl1.Focus();

                this.selectedBarInfo = (NeedCutBarCode)gridView_barCodes.GetRow(gridView_barCodes.GetSelectedRows()[0]);



                // 将选择的关联条码传回分切界面
                var selectedIndexes = gridView_barCodes.GetSelectedRows();
                var selectedItems = selectedIndexes.Select(i => oriBarCodes[i]);
                outputBarCodes = selectedItems.ToList();

                foreach (var sel in outputBarCodes)
                {
                    if (string.IsNullOrEmpty(sel.usedMeter) || string.IsNullOrEmpty(sel.usedWeight))
                    {
                        XtraMessageBox.Show("请填写米数与重量");
                        return;
                    }

                    // 使用米数和重量，不能大于原条码米数和重量
                    var check_used_meter = 0.0m;
                    var check_used_weight = 0.0m;

                    var check_ori_meter = 0.0m;
                    var check_ori_weight = 0.0m;

                    decimal.TryParse(sel.usedMeter, out check_used_meter);
                    decimal.TryParse(sel.usedWeight, out check_used_weight);

                    decimal.TryParse(sel.proMeter, out check_ori_meter);
                    decimal.TryParse(sel.proWeight, out check_ori_weight);

                    if (check_used_meter > check_ori_meter)
                    {
                        XtraMessageBox.Show("使用米数不得大于关联条码原米数");
                        return;
                    }

                    if (check_used_weight > check_ori_weight)
                    {
                        XtraMessageBox.Show("使用重量不得大于关联条码原重量");
                        return;
                    }

                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void gridView_barCodes_DoubleClick(object sender, EventArgs e)
        {
            this.selectedBarInfo = (NeedCutBarCode)gridView_barCodes.GetRow(gridView_barCodes.GetSelectedRows()[0]);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}